var Item = cc.Class({
  name: 'Item',
  properties: {
    id: 0,
    hp: 1,
    iconSF: cc.Sprite
  }
});

cc.Class({
  extends: cc.Component,
  properties: {
    items: {
      default: [],
      type: Item
    },
    itemPrefab: cc.Prefab
  },
  onLoad() {
    // this.init();
  },

  init(ctl) {
    // 随机添加一行
    this.EndlessGameCtl = ctl;
    // 随机函数
    const randomInt = (min, max) =>
      Math.floor(Math.random() * (max - min) + min);

    this.EndlessGameCtl.EndlessGameModel.level++;

    let screenWidth = this.node.width; //cc.winSize.width

    let { width, height } = this.itemPrefab.data;

    this.spacing = this.EndlessGameCtl.EndlessGameModel.BlockSpacing;

    // 动态生成items的长度
    // let BlockLen = randomInt(7, 7);
    // 动态计算一行多少个
    this.cols = parseInt(screenWidth / (width + this.spacing));
    // 左右动态间距
    let interval =
      (screenWidth - this.cols * (width + this.spacing) + this.spacing) / 2;

    for (let i = 0; i < this.cols; ++i) {
      let probability = randomInt(1, 10);
      if (probability > 3) {
        //.6概率进行预制体渲染
        let item = cc.instantiate(this.itemPrefab);
        this.node.addChild(item);

        item.x =
          (i % this.cols) * (item.width + this.spacing) +
          interval +
          item.width / 2;

        item.y =
          -Math.floor(i / this.cols) * (item.height + this.spacing) -
          item.height / 2;

        // 目前先是随机的,后期加入关卡和难度更改
        let type = randomInt(1, 100);
        if (type <= 80) {
          type = 0;
        } else if (type > 80) {
          type = 1;
        }

        item.getComponent('BlockTemplate').init({
          id: Date.now(),
          type,
          iconSF: randomInt(1, 5),
          hp: randomInt(1, 5) * this.EndlessGameCtl.EndlessGameModel.level
        });
      } else {
        continue;
      }
      // let item = cc.instantiate(this.itemPrefab);
      // this.node.addChild(item);

      // // 左右动态间距
      // let interval =
      //   (screenWidth - this.cols * (width + this.spacing) + this.spacing) / 2;

      // item.x =
      //   (i % this.cols) * (item.width + this.spacing) +
      //   interval +
      //   item.width / 2;

      // item.y =
      //   -Math.floor(i / this.cols) * (item.height + this.spacing) -
      //   item.height / 2;

      // item.getComponent('BlockTemplate').init({
      //   id: Date.now(),
      //   iconSF: '',
      //   hp: randomInt(1, 9)
      // });
    }
  },

  // 随机分布在幕布上
  randerCoord() {
    let opt = {};
  }
});
